<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 15/11/21
 * Time: 下午9:32
 */
class Article extends MY_Agent_Controller
{
    public function index()
    {
        $limit = 20;
        $currentPage = (int)$this->input->get_post('per_page');
        $offset = ($currentPage < 1) ? 0 : ($currentPage - 1) * $limit;


        $name   = trim($this->input->get_post('name'));
        $cid    = (int)$this->input->get_post('cid');
        $time   = $this->input->get_post('create_time');
        $status = $this->input->get_post('status');
        $top    = $this->input->get_post('top');



        $where = array();
        $name && $where['name'] = $name;
        $cid && $where['cid'] = $cid;
        ($status || $status === '0') && $where['status'] = $status;
        ($top || $top === '0') && $where['top'] = $top;


        if ($time)
        {
            $eTime = explode('-', $time);//echo $eTime[0].'<br>';p(date('Y-m-d H:i:s', strtotime($eTime[0])));
            $where['create_time >='] = date('Y-m-d H:i:s', strtotime($eTime[0]));
            $where['create_time <='] = date('Y-m-d ', strtotime($eTime[1])).'23:59:59';
        }


        $this->load->model('Model_article', 'article');

        $totalNum = $this->article->count($where);
        $articleInfo = $this->article->lists($offset, $limit, $where);


        $this->load->library('pagination');
        $config = $this->page_config('Article/index', $totalNum, $limit, http_build_query($where));
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $articleData = $this->article->lists(0, 100000, array(), 'name');


        $this->load->model('Model_article_cate', 'ac');
        $cateData = $this->ac->lists(0, 10000);

        $data = array(
            'title'        => '资讯列表',
            'article_info' => $articleInfo,
            'article_data' => $articleData,
            'cate_data'    => sortData($cateData),
            'pageHtml'     => $pageHtml,
            'time'         => $time,
            'cid'          => $cid,
            'top'          => $top,
            'status'       => $status,
            'name'         => $name,
            'totalNum'     => $totalNum,
        );

        $this->load->view('agent/article/index', $data);
    }

    public function create()
    {
        $aid = (int)$this->uri->segment(3);


        $this->load->model('Model_article_cate', 'ac');
        $cateData = $this->ac->lists(0, 10000);

        $item = array();
        if ($aid)
        {
            $this->load->model('Model_article', 'article');
            $item = $this->article->getById($aid);
        }


        $data = array(
            'title'     => '编辑资讯',
            'data'      => $item,
            'cate_data' => sortData($cateData),
        );


        $this->load->view('agent/article/create', $data);
    }

    public function save()
    {
        $aid = (int)$this->input->get_post('aid');
        $pic = (int)$this->input->get_post('pic');


        $data = array(
            'name' => trim($this->input->get_post('name')),
            'cid' => (int)$this->input->get_post('cid'),
            'sort' => (int)$this->input->get_post('sort'),
            'title' => trim($this->input->get_post('title')),
            'keyword' => trim($this->input->get_post('keyword')),
            'descr' => trim($this->input->get_post('descr')),
            'status' => (int)$this->input->get_post('status'),
            'top'  => (int)$this->input->get_post('top'),
            'content' => trim($this->input->get_post('content')),
        );

        $pic && $data['pic'] = $pic;

        empty($data['title'])   && $data['title']   = $data['name'].'_微信公众号新闻资讯';
        empty($data['keyword']) && $data['keyword'] = $data['name'];
        empty($data['descr'])   && $data['descr']   = mb_substr(str_replace(array("\n", "\r", ' ', '	'), array('', '', '', ''), strip_tags($data['content'])), 0, 128);


        if (empty ($data['name']))
            show_error('资讯标题为空!');


        $this->load->model('Model_article', 'article');
        $rs = $this->article->save($data, $aid ? $aid : null);

        if (!$rs)
            show_error('保存资讯失败!');


        $this->load->helper('url');
        redirect(url('admin').'Article/index');
    }


    public function delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $aid = (int)$this->input->get_post('aid');

            if (!$aid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_article', 'article');
            $item = $this->article->getById($aid);

            if (empty ($item))
            {
                $response = error(700003);
                break;
            }


            $res = $this->article->delete(array('aid' => $aid));
            if (!$res)
            {
                $response = error(700005);
                break;
            }
        }while (false);


        $this->json_output($response);
    }


    public function audit()
    {
        $response = array('code' => '0', 'msg' => '回复成功');

        do {
            $aid = (int)$this->input->get_post('aid');
            $reply_content = $this->input->get_post('reply_content');
            $status = (int)$this->input->get_post('status');

            if (!$aid || empty ($reply_content))
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_article', 'article');
            $item = $this->article->getById($aid);

            if (empty ($item))
            {
                $response = error(700003);
                break;
            }


            if ($item['status'] == ARTICLE_STATUS_NOT_AUDIT)
            {
                $response = error(700004);
                break;
            }


            if (!empty ($item['reply_content']) || !empty ($item['reply_time']))
            {
                $response = error(700004);
                break;
            }


            $res = $this->comment->save(array('status' => $status), $aid ? $aid : null);
            if (!$res)
            {
                $response = error(900002);
                break;
            }
        }while (false);


        $this->json_output($response);
    }







    public function cate_index()
    {
        $limit = 20;
        $currentPage = (int)$this->input->get_post('per_page');
        $offset = ($currentPage < 1) ? 0 : ($currentPage - 1) * $limit;


        $this->load->model('Model_article_cate', 'ac');

        $totalNum = $this->ac->count();
        $cateInfo = $this->ac->lists($offset, $limit);


        $this->load->library('pagination');
        $config = $this->page_config('Article/cate_index', $totalNum, $limit);
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $data = array(
            'title'     => '资讯分类列表',
            'cate_info' => sortData($cateInfo),
            'pageHtml'  => $pageHtml,
            'totalNum'  => $totalNum,
        );


        $this->load->view('agent/article/cate_index', $data);
    }

    public function cate_create()
    {
        $cid = (int)$this->uri->segment(3);

        $this->load->model('Model_article_cate', 'ac');
        $cateData = $this->ac->lists(0, 10000);

        $item = array();
        if ($cid)
            $item = $this->ac->getById($cid);


        $data = array(
            'title'     => '编辑分类',
            'data'      => $item,
            'cate_data' => sortData($cateData),
        );

        $this->load->view('agent/article/cate_create', $data);
    }

    public function cate_save()
    {
        $cid = (int)$this->input->get_post('cid');


        $data = array(
            'name' => trim($this->input->get_post('name')),
            'parent_id' => (int)$this->input->get_post('parent_id'),
            'sort' => (int)$this->input->get_post('sort'),
            'title' => trim($this->input->get_post('title')),
            'keyword' => trim($this->input->get_post('keyword')),
            'descr' => trim($this->input->get_post('descr')),
            'depth' => trim($this->input->get_post('depth')),
        );


        if (empty ($data['name']))
            show_error('分类名称为空!');


        $this->load->model('Model_article_cate', 'ac');
        $rs = $this->ac->save($data, $cid ? $cid : null);

        if (!$rs)
            show_error('保存分类失败!');


        $this->load->helper('url');
        redirect(url('admin').'Article/cate_index');
    }


    public function cate_delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $cid = (int)$this->input->get_post('cid');

            if (!$cid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_article_cate', 'ac');
            $item = $this->ac->getById($cid);

            if (empty ($item))
            {
                $response = error(700000);
                break;
            }


            $this->load->model('Model_article', 'article');
            $item = $this->article->getOne(array('cid' => $cid));
            if (!empty ($item))
            {
                $response = error(700002);
                break;
            }


            $res = $this->ac->delete(array('cid' => $cid));
            if (!$res)
            {
                $response = error(700001);
                break;
            }
        }while (false);


        $this->json_output($response);
    }
}